package cn.zn.controller.system;

import cn.hutool.json.JSONObject;
import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.flowTemplate.entity.SysFlowTemplate;
import cn.zn.flowTemplate.service.SysFlowTemplateService;
import cn.zn.flowTemplate.vo.SysFlowTemplateEditVo;
import cn.zn.flowTemplate.vo.SysFlowTemplatePageVo;
import cn.zn.framework.utils.SecurityUtil;
import cn.zn.user.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zn-admin
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/flow/template")
public class SysFlowTemplateController {

    @Autowired
    private SysFlowTemplateService flowTemplateService;

    @PreAuthorize("@ss.hasPermi('system:flow:list')")
    @GetMapping("/list")
    public ResultData list(SysFlowTemplatePageVo vo) {
        Object list = flowTemplateService.selectFlowTemplateList(vo);
        return ResultUtil.success(list);
    }

    @GetMapping(value = "/{id}")
    public ResultData info(@PathVariable String id) {
        SysFlowTemplate template = flowTemplateService.selectFlowTemplateInfo(id);
        return ResultUtil.success(template);
    }

    @GetMapping(value = "/code")
    public ResultData infoByCode(String code) {
        SysUser user = SecurityUtil.getTheUser();
        SysFlowTemplate template = flowTemplateService.selectFlowTemplateInfo(code, user.getCorpId());
        return ResultUtil.success(template);
    }

    /**
     * 新增流程模板
     */
    @PreAuthorize("@ss.hasPermi('system:flow:add')")
    @Log(title = "流程模板", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated SysFlowTemplateEditVo vo) {
        flowTemplateService.insertFlowTemplate(vo);
        return ResultUtil.success();
    }

    /**
     * 修改流程模板
     */
    @PreAuthorize("@ss.hasPermi('system:flow:edit')")
    @Log(title = "流程模板", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{id}")
    public ResultData edit(@PathVariable String id, @RequestBody @Validated SysFlowTemplateEditVo vo) {
        flowTemplateService.updateFlowTemplate(id, vo);
        return ResultUtil.success();
    }

    @GetMapping("/tables")
    public ResultData allTables() {
        List<String> list = flowTemplateService.selectAllTables();
        return ResultUtil.success(list);
    }

    @GetMapping("/columns/{table}")
    public ResultData allColumns(@PathVariable String table) {
        List<JSONObject> list = flowTemplateService.selectAllColumnsByTable(table);
        return ResultUtil.success(list);
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:flow:remove')")
    @Log(title = "流程模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable String[] ids) {
        flowTemplateService.deleteFlowTemplateByIds(ids);
        return ResultUtil.success();
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:flow:edit')")
    @Log(title = "流程模板", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{id}")
    public ResultData changeStatus(@PathVariable String id, @RequestBody SysFlowTemplateEditVo vo) {
        flowTemplateService.updateFlowTemplateStatus(id, vo);
        return ResultUtil.success();
    }

}
